package com.hbx.platform.auth.controller;

import com.hbx.platform.auth.domain.Permission;
import com.hbx.platform.auth.query.PermissionQuery;
import com.hbx.platform.auth.service.IPermissionService;
import com.hbx.platform.base.annotation.AutoPermission;
import com.hbx.platform.base.utils.AjaxResult;
import com.hbx.platform.base.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
@AutoPermission(name = "权限管理")
public class PermissionController {
    @Autowired
    public IPermissionService permissionService;


    /**
     * 保存和修改公用的
     * @param permission  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @AutoPermission(name = "添加或修改权限",desc = "id为null添加，反之修改")
    public AjaxResult saveOrUpdate(@RequestBody Permission permission){
        try {
            if( permission.getId()!=null){
                    permissionService.update(permission);
                    return AjaxResult.me().setMessage("修改成功");
            }else {
                 permissionService.save(permission);
                 return AjaxResult.me().setMessage("添加成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("报错："+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @AutoPermission(name = "删除权限",desc = "根据id删除权限")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            permissionService.delete(id);
            return AjaxResult.me().setMessage("删除成功");
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable("id")Long id) {
        try {
            Permission permission = permissionService.queryById(id);
            return AjaxResult.me().setData(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult getAll(){
        try {
            List< Permission> list = permissionService.getAll();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult page(@RequestBody PermissionQuery query) {
        try {
            PageList<Permission> pageList = permissionService.page(query);
            return AjaxResult.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！"+e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @AutoPermission(name = "批量删除权限",desc = "批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            Integer count = permissionService.batchDelete(ids);
            return AjaxResult.me().setMessage("删除了"+count+"条记录");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败");
        }
    }

    @GetMapping("/tree")
    public AjaxResult getTree(){
        try {
            List<Permission> permissions = permissionService.getTree();
            return AjaxResult.me().setData(permissions);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("报错："+e.getMessage());
        }
    }
}
